package com.zhaoxi.project.wms.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.zhaoxi.common.core.text.Convert;
import com.zhaoxi.common.utils.DateUtils;
import com.zhaoxi.common.utils.DictUtils;
import com.zhaoxi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zhaoxi.project.wms.mapper.WmsWorkshopLineMapper;
import com.zhaoxi.project.wms.domain.WmsWorkshopLine;

/**
 * 厂区|生产车间l生产流水线Service业务层处理
 * 
 * @author zhaoxi
 * @date 2023-11-10
 */
@Service
public class WmsWorkshopLineServiceImpl
{
    @Autowired
    private WmsWorkshopLineMapper wmsWorkshopLineMapper;

    /**
     * 查询厂区|生产车间l生产流水线
     * 
     * @param id 厂区|生产车间l生产流水线主键
     * @return 厂区|生产车间l生产流水线
     */
    
    public WmsWorkshopLine selectWmsWorkshopLineById(Long id)
    {
        return wmsWorkshopLineMapper.selectWmsWorkshopLineById(id);
    }

    /**
     * 查询厂区|生产车间l生产流水线列表
     * 
     * @param wmsWorkshopLine 厂区|生产车间l生产流水线
     * @return 厂区|生产车间l生产流水线
     */
    
    public List<WmsWorkshopLine> selectWmsWorkshopLineList(WmsWorkshopLine wmsWorkshopLine)
    {
            //parentId==null时，根据搜索按钮搜索
            if(wmsWorkshopLine != null && wmsWorkshopLine.getFactoryId()!=null && wmsWorkshopLine.getParentId()==null){
                wmsWorkshopLine.setId(wmsWorkshopLine.getFactoryId());
            }else if(wmsWorkshopLine != null && wmsWorkshopLine.getParentId() != null && wmsWorkshopLine.getParentId()==0L )
            //parentId==0时，查找全部厂区名称
            {
                wmsWorkshopLine = new WmsWorkshopLine();
                wmsWorkshopLine.setParentId(0L);
            }
            return wmsWorkshopLineMapper.selectWmsWorkshopLineList(wmsWorkshopLine);
    }

    /**
     * 新增厂区|生产车间l生产流水线
     * 
     * @param wmsWorkshopLine 厂区|生产车间l生产流水线
     * @return 结果
     */
    
    public int insertWmsWorkshopLine(WmsWorkshopLine wmsWorkshopLine)
    {
        wmsWorkshopLine.setCreateTime(DateUtils.getNowDate());
        //添加的是非厂区时
        if(wmsWorkshopLine.getParentId()!=0){
            WmsWorkshopLine parentWorkShopLine = wmsWorkshopLineMapper.selectWmsWorkshopLineById(wmsWorkshopLine.getParentId());
            if (parentWorkShopLine == null){
                return 0;
            }else if(parentWorkShopLine.getEnableFlag().equals("1")){
                return 0;
            }
            wmsWorkshopLine.setAncestors(parentWorkShopLine.getAncestors() + wmsWorkshopLine.getParentId() + ",");
            return wmsWorkshopLineMapper.insertWmsWorkshopLine(wmsWorkshopLine);
        }else{
            //添加的是厂区
            wmsWorkshopLine.setAncestors("0,");
            return wmsWorkshopLineMapper.insertWmsWorkshopLine(wmsWorkshopLine);
        }
    }

    /**
     * 修改厂区|生产车间l生产流水线
     * 
     * @param wmsWorkshopLine 厂区|生产车间l生产流水线
     * @return 结果
     */
    public int updateWmsWorkshopLine(WmsWorkshopLine wmsWorkshopLine) {

        wmsWorkshopLine.setUpdateTime(DateUtils.getNowDate());

        WmsWorkshopLine newParentLine = wmsWorkshopLineMapper.selectWmsWorkshopLineById(wmsWorkshopLine.getParentId());
        WmsWorkshopLine oldLine = wmsWorkshopLineMapper.selectWmsWorkshopLineById(wmsWorkshopLine.getId());
        if (StringUtils.isNotNull(newParentLine) && StringUtils.isNotNull(oldLine)) {
            String newAncestors = newParentLine.getAncestors() + newParentLine.getId() + ",";
            String oldAncestors = oldLine.getAncestors();
            wmsWorkshopLine.setAncestors(newAncestors);
            updateLineChildren(wmsWorkshopLine.getId(), newAncestors, oldAncestors);
        }
        int result = wmsWorkshopLineMapper.updateWmsWorkshopLine(wmsWorkshopLine);
        if ( DictUtils.getDictValue("sys_state","是").equals(wmsWorkshopLine.getEnableFlag()) && StringUtils.isNotEmpty(wmsWorkshopLine.getAncestors())
                && !StringUtils.equals("0", wmsWorkshopLine.getAncestors())) {
            //若该部门修改成了启用状态，则其所有上级部门均修改成启用状态
            updateParentLineEnableFlagNormal(wmsWorkshopLine);
        }
        if ( DictUtils.getDictValue("sys_state","否").equals(wmsWorkshopLine.getEnableFlag()) && StringUtils.isNotEmpty(wmsWorkshopLine.getAncestors())) {
            //若该部门修改成了禁用状态，则其所有下级部门均修改成禁用状态
            updateParentLineEnableFlagDisable(wmsWorkshopLine);
        }
        return result;
    }

    /**
     * 修改该厂区的父厂区启用状态
     * @param wmsWorkshopLine
     */
    private void updateParentLineEnableFlagNormal(WmsWorkshopLine wmsWorkshopLine) {
        String ancestors = wmsWorkshopLine.getAncestors();
        Long[] lineIds = Convert.toLongArray(ancestors);
        wmsWorkshopLineMapper.updateLineStatusNormal(lineIds);
    }

    /**
     * 修改该厂区的父厂区禁用状态
     * @param wmsWorkshopLine
     */
    private void updateParentLineEnableFlagDisable(WmsWorkshopLine wmsWorkshopLine) {
        List<WmsWorkshopLine> children = wmsWorkshopLineMapper.selectChildrenLineById(wmsWorkshopLine.getId());
        List<Long> list = new ArrayList<>();
        for (WmsWorkshopLine item : children){
            list.add(item.getId());
        }
        Long[] lineIds = list.toArray(new Long[list.size()]);
        wmsWorkshopLineMapper.updateLineStatusDisable(lineIds);
    }

    /**
     * 修改子元素的关系
     * @param newAncestors
     * @param oldAncestors
     */
    public void updateLineChildren(Long lineId, String newAncestors, String oldAncestors) {
        List<WmsWorkshopLine> children = wmsWorkshopLineMapper.selectChildrenLineById(lineId);
        for(WmsWorkshopLine child : children){
            child.setAncestors((child.getAncestors().replaceFirst(oldAncestors,newAncestors)));
        }
        if (children.size() > 0){
            wmsWorkshopLineMapper.updateLineChildren(children);
        }
    }

    /**
     * 批量删除厂区|生产车间l生产流水线
     * 
     * @param id 需要删除的厂区|生产车间l生产流水线主键
     * @return 结果
     */
    public int deleteWmsWorkshopLineByIds(Long id)
    {
        List<WmsWorkshopLine> children = wmsWorkshopLineMapper.selectChildrenLineById(id);
        List<Long> list = new ArrayList<>();
        list.add(id);
        for (WmsWorkshopLine wmsWorkshopLine : children){
                list.add(wmsWorkshopLine.getId());
        }
        Long[] ids = list.toArray(new Long[list.size()]);
        return wmsWorkshopLineMapper.deleteWmsWorkshopLineByIds(ids);
    }

    /**
     * 删除厂区|生产车间l生产流水线信息
     * 
     * @param id 厂区|生产车间l生产流水线主键
     * @return 结果
     */
    
    public int deleteWmsWorkshopLineById(Long id)
    {
        return wmsWorkshopLineMapper.deleteWmsWorkshopLineById(id);
    }


}
